package com.hrt.datasender.datacollect.drive.modbus.tcp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.btcode.common.BitType;
import com.hrt.datasender.datacollect.drive.DriveConfigModel;
import com.hrt.datasender.datacollect.drive.DriveParameterMetadata;
import com.hrt.datasender.datadao.model.BcDeviceConfigModel;
import com.hrt.datasender.datadao.model.BcDeviceMonitorParamModel;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.datasender.datadao.model.BdSourceModel;
import com.hrt.serial.modbus.ModbusDataDecodeTCP;
import com.hrt.serial.modbus.ModbusDataModel;

/**
 * modbusTCP驱动
 * 
 * @author wencheng
 *
 */
public class ModbusTCPDrive extends NetBaseDrive {

    protected ModbusDataDecodeTCP decode = new ModbusDataDecodeTCP();

    @Override
    public String getNote() {
        return null;
    }

    @Override
    public String getDriveName() {
        return "ModbusTCP驱动";
    }

    @Override
    public String getConsStr() {
        return "0/10 * * * * ?";
    }

    @Override
    public List<BdSourceModel> processNetExportData(byte[] data, HashMap<Integer, String> paramCodeMap,
            DriveConfigModel deviceConfigs) {

        List<ModbusDataModel> modbusDataModels = decode.dataModelDecode(data, data.length, deviceConfigs);
        ModbusDataModel modbusDataModel = modbusDataModels.get(0);
        double[] doubleData = modbusDataModel.getData();

        List<BdSourceModel> sourceModels = new ArrayList<>();

        for (int i = 0; i < doubleData.length; i++) {

            String paramCodeBus = paramCodeMap.get(i);

            if (paramCodeBus == null) {
                continue;
            }

            BdSourceModel sourModel = new BdSourceModel();
            sourModel.setParamCodeBus(paramCodeBus);
            sourModel.setDatatime(new Date());
            sourModel.setMeasRe(doubleData[i]);
            sourceModels.add(sourModel);
        }

        return sourceModels;
    }

    @Override
    public List<DriveParameterMetadata> getParameterMetadata() {
        List<DriveParameterMetadata> params = new ArrayList<>();
        params.add(new DriveParameterMetadata("ip", "IP"));
        params.add(new DriveParameterMetadata("port", "端口"));
        params.add(new DriveParameterMetadata("slaveNo", "从站编号"));
        params.add(new DriveParameterMetadata("startNo", "起始位置"));
        params.add(new DriveParameterMetadata("dataLength", "数据长度"));
        params.add(new DriveParameterMetadata("driveInstanceId", "驱动实例ID"));
        params.add(new DriveParameterMetadata("functionCode", "功能码"));
        return params;
    }

    @Override
    protected void beforeInit(BcDevicesModel device, List<BcDeviceMonitorParamModel> params,
            List<BcDeviceConfigModel> configs) {
        // TODO Auto-generated method stub

    }

}
